package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 将请求信息封装成该对象
 */
public class Request {
    private InputStream inputStream;
    private String method;//请求方式GET/POST。。。
    private String url;// 请求的路径

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    public Request(){}
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        //从输入流中获取请求信息
        int count = 0;
        while (count == 0){
            count = inputStream.available();//输入流的长度
        }
        byte[] bytes = new byte[count];//输入流长度
        inputStream.read(bytes);
        String inputStr = new String(bytes);
//        System.out.println("接收到的数据\n"+inputStr);
        /**
         * 接收到的数据
         * GET / HTTP/1.1
         * Host: localhost:8080
         * Connection: keep-alive
         * Cache-Control: max-age=0
         * Upgrade-Insecure-Requests: 1
         * User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
         * Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*;
         */
        //第一行数据  GET / HTTP/1.1
        String firstLine = inputStr.split("\\n")[0];
        String[] strings = firstLine.split(" ");
        this.method = strings[0];
        this.url = strings[1];
    }
}
